<?php

namespace app\data\controller;
use think\admin\Controller;
use think\admin\helper\QueryHelper;
use app\data\model\DataBreed;
use app\data\model\DataBreedWeightRanges;
/**
 * 品种管理
 * @class Hotel
 * @package app\data\controller
 */
class Breed extends Controller
{
    /**
     * 品种管理
     * @menu true
     * @auth true
     */
    public function index()
    {
        DataBreed::mQuery()->layTable(function () {
            $this->title = '品种管理';
        }, function (QueryHelper $query) {
            $query->where(['is_deleted' => 0]);
            // 数据列表搜索过滤
            $query->equal('status')->dateBetween('create_at#create_at');
            $query->like('breed_name');
        });
    }

    /**
     * 添加品种
     * @auth true
     */
    public function add()
    {
        DataBreed::mForm('form');
    }

    /**
     * 编辑品种
     * @auth true
     */
    public function edit()
    {
        DataBreed::mForm('form');
    }

    /**
     * 删除品种
     * @auth true
     */
    public function remove()
    {
        DataBreed::mDelete();
    }
    /**
     * 设置数据
     * @auth true
     */
    function weight(){
        $id =input('param.id/d');
        $query = $this->_query('data_breed_weight_ranges');
        $query->dateBetween('create_at');
        $query->where('breed_id','=',$id)->order('month asc')->page();
    }
    /**
     * 添加数据
     * @auth true
     */
    function add_data(){
        $this->breed_id = input('param.breed_id');
        if ($this->request->isPost()) {
            $map = [
                'breed_id'=>input('param.breed_id'),
                'gender'=>input('param.gender'),
                'month'=>input('param.month')
            ];
            $chek = DataBreedWeightRanges::mk()->where($map)->count();
            if($chek>0){
                $this->error("数据已存在,请勿重复添加");
            }else{
                $map['min_weight']=input('param.min_weight');
                $map['max_weight']=input('param.max_weight');
                $map['toilet_times']=input('param.toilet_times');
                DataBreedWeightRanges::mk()->insert($map);
                $this->success('添加成功');
            }
        }else{
            $this->fetch();
        }
    }

    /**
     * 编辑数据
     * @auth true
     */
    public function edit_data()
    {
        DataBreedWeightRanges::mForm('form_data');
    }

    /**
     * 删除数据
     * @auth true
     */
    public function remove_data()
    {
        DataBreedWeightRanges::mDelete();
    }
    /**
     * 修改用户状态
     * @auth true
     */
    public function state()
    {
        DataBreed::mSave($this->_vali([
            'is_tj.in:0,1'  => '状态值范围异常！',
            'is_tj.require' => '状态值不能为空！',
        ]));
    }
}
